<template>
  <div class="panna-dialog_medium">
    <div class="main-container" style="height: 100%">
      <el-button size="mini" type="primary" @click="add">{{
        operateType.add
      }}</el-button>
      <el-button size="mini" type="primary" @click="edit">{{
        operateType.edit
      }}</el-button>
      <el-button size="mini" type="danger" @click="del">{{
        operateType.delete
      }}</el-button>
      <el-table
        ref="CustomerDetectionItemPrice"
        :data="dataList"
        element-loading-text="Loading"
        fit
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>

        <el-table-column
          align="center"
          :label="customer.serialNumber"
          width="50"
        >
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column prop="detectionItemName" :label="customer.itemName">
        </el-table-column>
        <el-table-column prop="agreementPrice" :label="customer.unitPrice">
          <template slot-scope="scope">
            <el-input
              v-if="editPrice"
              v-model="scope.row.agreementPrice"
              clearable
            />
            <span v-else>{{ scope.row.agreementPrice }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <span
      v-if="editPrice"
      slot="footer"
      class="base-dialog-footer"
      style="padding-top: 18px"
    >
      <el-button type="primary" @click="submit">{{
        operateType.save
      }}</el-button>
      <el-button @click="editPrice = false">{{ operateType.cancel }}</el-button>
    </span>
    <CustomerDetectionItemAdd
      v-if="isShowDialog"
      ref="CustomerDetectionItemAdd"
      :is-show-dialog.sync="isShowDialog"
      :edit-id="editId"
      @initdata="initdata"
    />
  </div>
</template>

<script>
import CustomerDetectionItemAdd from "./CustomerDetectionItemAdd.vue";
import {
  deleteCustomer,
  updates,
  getListNoPage,
} from "../../../api/SysBusiness/CustomerDetectionItemPrice";
export default {
  name: "CustomerDetectionItemPrice",
  components: { CustomerDetectionItemAdd },
  props: {
    editId: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      multipleSelection: [],
      editPrice: false,
      isShowDialog: false,
      dataList: [],
    };
  },
  computed: {
    operateType() {
      return this.$t("ModuleSysBusiness.OperateType") || {};
    },
    customer() {
      return this.$t("Customer") || {};
    },
  },
  created() {
    this.initdata();
  },
  methods: {
    submit() {
      if (this.multipleSelection.length > 0) {
        var obj = JSON.parse(JSON.stringify(this.multipleSelection));
        for (var i = 0; i < obj.length; i++) {
          obj[i].agreementPrice = obj[i].agreementPrice * 100;
        }
        updates(obj)
          .then((res) => {
            this.editPrice = false;
          })
          .catch((res) => {});
      } else {
        this.editPrice = false;
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    cancelForm() {},
    // 新增客户信息
    add() {
      this.isShowDialog = true;
    },
    edit(row) {
      this.editPrice = true;
    },
    del(row) {
      if (this.multipleSelection.length <= 0) {
        this.$message({
          type: "info",
          message: this.$t("Customer.selectData"),
        });
        return;
      }

      this.$confirm(
        this.$t("Customer.sureDeleteItem"),
        this.$t("Customer.deleteDetectionItems"),
        {
          type: "warning",
          confirmButtonText: this.$t("ModuleSysBusiness.OperateType.confirm"),
          okType: "danger",
          center: true,
          cancelButtonText: this.$t("ModuleSysBusiness.OperateType.cancel"),
        }
      )
        .then(() => {
          deleteCustomer(this.multipleSelection).then((res) => {
            this.initdata();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("Customer.cancelledDeletion"),
          });
        });
    },
    initdata() {
      var obj = {};
      obj.customerId = this.editId;
      getListNoPage(obj)
        .then((res) => {
          this.dataList = res.data;
        })
        .catch((res) => {});
    },
    // 切换页码
    changePage(page) {
      this.pageInfo.pageIndex = page;
    },
    sizeChange(val) {
      this.pageInfo.pageIndex = 1;
      this.pageInfo.pageSize = val;
    },
  },
};
</script>
